define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'Wqdoc/index',
                    add_url: 'Wqdoc/add',
                    edit_url: 'Wqdoc/edit',
                    del_url: 'Wqdoc/del',
                    multi_url: 'Wqdoc/multi',
                    table: 'wqdocument',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: true,
                search: false,
                columns: [
                    [{checkbox: true},
                    {field: 'id', title: __('Id'), operate:false },
                    {field: 'title', title: __('标题')},
                    {field: 'type', title: __('Type'), operate:false, searchList: Config.typeList, formatter: Table.api.formatter.label},
                    {field: 'createtime', title: __('创建时间'), operate:false, formatter: Table.api.formatter.datetime},
                    {field: 'status', title: __('Status'), operate: false, formatter: Table.api.formatter.status},
                    {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
             //绑定TAB事件
             $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    
                    if(typeStr !='all'){
                        params.filter = typeof params.filter === 'Object' ? params.filter : (params.filter ? JSON.parse(params.filter) : {});
                        params.op = typeof params.op === 'Object' ? params.op : (params.op ? JSON.parse(params.op) : {});
                        params.filter = $.extend({}, params.filter, {type: typeStr});
                        params.op = $.extend({}, params.op, {type: '='});
                        params.filter = JSON.stringify(params.filter);
                        params.op = JSON.stringify(params.op);
                    }
                    return params;
                   
                };
                table.bootstrapTable('refresh', {});
                return false;

            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($('form[role=form]'));
            }
        }
    };
    return Controller;
});